package cn.lyjuan.android.mylibrary.view.recycler;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;

/**
 * Created by chad on 07/11/2017.
 */

public class RecyclerListView extends android.support.v7.widget.RecyclerView
{
    private static final String TAG = RecyclerListView.class.getSimpleName();

    private AbstractItemDecoration.Cache cache;

    @Override
    public void addItemDecoration(ItemDecoration decor)
    {
        if (decor instanceof AbstractItemDecoration)
        {
            AbstractItemDecoration descorTmp = (AbstractItemDecoration) decor;
            descorTmp.cache = this.cache;
        }

        super.addItemDecoration(decor);
    }

    public RecyclerListView(Context context)
    {
        super(context);
    }

    public RecyclerListView(Context context, @Nullable AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        cacheAttrs(context, attrs);
    }

    public RecyclerListView(Context context, @Nullable AttributeSet attrs)
    {
        super(context, attrs);
        cacheAttrs(context, attrs);
    }

    private void cacheAttrs(Context context, @Nullable AttributeSet attrs)
    {
        cache = AbstractItemDecoration.cacheAttrs(context, attrs);
        if (this.getLayoutManager() instanceof LinearLayoutManager)
        {
            ((LinearLayoutManager) this.getLayoutManager()).setOrientation(cache.orientation);
        }
    }
}
